package cn.com.coding.app;

import cn.com.coding.common.lang.ResT;
import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.common.utils.RedisUtil;
import cn.com.coding.system.domin.ProductAnalyseEntity;
import cn.com.coding.system.domin.ProductCategoryEntity;
import cn.com.coding.system.domin.ProductInfoEntity;
import cn.com.coding.system.domin.dto.ProductAnalyseDto;
import cn.com.coding.system.service.ProductAnalyseService;
import cn.com.coding.system.service.ProductCategoryService;
import cn.com.coding.system.service.ProductInfoService;
import cn.hutool.core.map.MapUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("cat/productAnalyse")
public class ProductAnalyseController extends BaseController {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private ProductAnalyseService productAnalyseService;

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @RequestMapping("/login/success")
    public ResT getLoginSuccess(@RequestBody Map<String, Object> params) {
        return ResT.ok().put("getLoginSuccess", redisUtil.hget("mxz_user_login_success_record", String.valueOf(LocalDate.now())));
    }

    @RequestMapping("/register")
    public ResT getRegister(@RequestBody Map<String, Object> params) {
        return ResT.ok().put("getRegister", redisUtil.hget("mxz_user_register_record", String.valueOf(LocalDate.now())));
    }

    @RequestMapping("/login/fail")
    public ResT getLoginFail(@RequestBody Map<String, Object> params) {
        return ResT.ok().put("getLoginFail", redisUtil.hget("mxz_user_login_fail_record", String.valueOf(LocalDate.now())));
    }

    @RequestMapping("/order")
    public ResT getOrder(@RequestBody Map<String, Object> params) {
        return ResT.ok().put("getOrder", redisUtil.hget("mxz_user_add_order", String.valueOf(LocalDate.now())));
    }

    @RequestMapping("/order/sum")
    public ResT getOrderSum(@RequestBody Map<String, Object> params) {
        return ResT.ok().put("getOrderSum", redisUtil.hget("mxz_user_order_sum", String.valueOf(LocalDate.now())));
    }

    /**
     * 分页
     */
    @RequestMapping("/list")
    public ResT list(@RequestBody Map<String, Object> params) {
        PageUtil page = productAnalyseService.queryPage(params);

        return ResT.ok().put("productAnalysePage", page);
    }

    /**
     * all
     */
    @RequestMapping("/all")
    public ResT all() {
        List<ProductAnalyseDto> productAnalyseDtoList = productAnalyseService.list().stream().map(e -> {
            ProductAnalyseDto productAnalyseDto = new ProductAnalyseDto();
            ProductInfoEntity productInfo = productInfoService.getById(e.getProductPid());
            productAnalyseDto.setProductName(productInfo.getProductName());
            ProductCategoryEntity productCategory = productCategoryService.getById(productInfo.getCategoryPid());
            productAnalyseDto.setCateGoryName(productCategory.getCategoryName());
            BeanUtils.copyProperties(e, productAnalyseDto);
            productAnalyseDto.setNum(e.getNum() * 100000);
            return productAnalyseDto;
        }).collect(Collectors.toList());
        return ResT.ok().put("productAnalyseAll", productAnalyseDtoList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{pid}")
    public ResT info(@PathVariable("pid") String pid) {
        ProductAnalyseEntity productAnalyse = productAnalyseService.getById(pid);
        productAnalyseService.updateById(productAnalyse);
        return ResT.ok().setData(MapUtil.builder().put("productAnalyse", productAnalyse).build());
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResT save(@RequestBody ProductAnalyseEntity productAnalyse) {

        productAnalyseService.save(productAnalyse);

        return ResT.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResT update(@RequestBody ProductAnalyseEntity productAnalyse) {

        productAnalyse.setUpdateTime(new Date());
        productAnalyseService.updateById(productAnalyse);

        return ResT.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResT delete(@RequestBody String[] pids) {
        List<ProductAnalyseEntity> list = new ArrayList<>(pids.length);

        Arrays.stream(pids).forEach(e -> {
            ProductAnalyseEntity productAnalyseEntity = productAnalyseService.getById(e);
            productAnalyseEntity.setUpdateTime(new Date());
            list.add(productAnalyseEntity);
        });

        productAnalyseService.saveOrUpdateBatch(list);

        return ResT.ok();
    }


    private static final String ORDERDETAIL = "/Users/inke219223m/mxz-code/gitee/cat-leader/cat-common/src/main/java/cn/com/coding/common/utils/order.txt";


    @RequestMapping("/txtToDb")
    public ResT txtToDb() {
        String filePath = ORDERDETAIL;
        FileInputStream fin = null;
        BufferedReader buffReader = null;
        try {
            fin = new FileInputStream(filePath);
            InputStreamReader reader = new InputStreamReader(fin);
            buffReader = new BufferedReader(reader);
            String strTmp = "";
            LocalDate date = LocalDate.now();
            redisUtil.hincr("mxz_user_login_record", String.valueOf(date), 1);
            while ((strTmp = buffReader.readLine()) != null) {
                ProductAnalyseEntity productAnalyseEntity = new ProductAnalyseEntity();

                productAnalyseEntity.setProductPid(strTmp.split("\t")[0]);
                productAnalyseEntity.setNum(Integer.valueOf(strTmp.split("\t")[1]));
                productAnalyseService.save(productAnalyseEntity);
            }

            return ResT.ok();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                buffReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ResT.error();
    }

    public static void main(String[] args) {
        new ProductAnalyseController().txtToDb();
    }
}